package com.msb.c08;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xw
 * @create: 2022/9/29 9:56
 * @description:volatile并不能保证多个线程共同修改running变量时所带来的不一致问题，也就是说volatile不能替代synchronized
 */
public class VolatileNotSync {
    volatile int count = 0;
    void m(){
        for (int i = 0; i < 10000; i++) {
            count++;
        }
    }

    public static void main(String[] args) {
        VolatileNotSync t = new VolatileNotSync();
        List<Thread> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            list.add(new Thread(t::m,"thread - " + i));
        }
        list.forEach((o)->o.start());

        list.forEach((o)->{
            try {
                o.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(t.count);
    }
}
